package com.albertyjz.yygh.hosp.service.impl;

import com.albertyjz.yygh.hosp.repository.ScheduleRepository;
import com.albertyjz.yygh.hosp.service.ScheduleService;
import com.albertyjz.yygh.model.hosp.Department;
import com.albertyjz.yygh.model.hosp.Schedule;
import com.albertyjz.yygh.vo.hosp.BookingScheduleRuleVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleRepository scheduleRepository;
    //用上面的方法也行，这里选择用mongotemplate来实现
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Map<String, Object> stringObjectMap) {
        String mapString= JSONObject.toJSONString(stringObjectMap);
        Schedule schedule = JSONObject.parseObject(mapString, Schedule.class);
        //根据排班编号和医院编号，判断是否有这个数据
        Schedule scheduleExist=
                scheduleRepository.getScheduleByHoscodeAndHosScheduleId(schedule.getHoscode(),schedule.getHosScheduleId());
        if(scheduleExist != null) {
            scheduleExist.setUpdateTime(new Date());
            scheduleExist.setIsDeleted(0);
            scheduleExist.setStatus(1);
            scheduleRepository.save(scheduleExist);
        }else {
            schedule.setCreateTime(new Date());
            schedule.setUpdateTime(new Date());
            schedule.setIsDeleted(0);
            schedule.setStatus(1);
            scheduleRepository.save(schedule);
        }
    }

    @Override
    public Map<String, Object> getRuleSchedule(long page, long limit, String hoscode, String depcode) {
        //1 根据医院编号和科室编号查询
        Criteria criteria=Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode);
        //2 根据工作的日期workDate进行分组
        Aggregation agg=Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group("workDate").first("workDate").as("workDate")
                //3 统计号原的数量
                .count().as("docCount")
                .sum("reservedNumber").as("reservedNumber")
                .sum("availableNumber").as("availableNumber"),
                Aggregation.sort(Sort.Direction.DESC,"workDate"),
                //4 实现分页
                Aggregation.skip((page-1)*limit),
                Aggregation.limit(limit)
        );

        AggregationResults<BookingScheduleRuleVo> aggregate = mongoTemplate.aggregate(agg, Schedule.class, BookingScheduleRuleVo.class);
        List<BookingScheduleRuleVo> mappedResults=aggregate.getMappedResults();

        return null;
    }
}
